package com.ganee.agora.adapter

import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ganee.agora.R
import com.ganee.agora.manager.RecyclerViewHolder
import com.ganee.agora.model.CommentEntity
import com.ganee.agora.model.HomeListBean
import com.ganee.agora.widget.ratingbar.AndRatingBar
import com.ganee.baselib.widget.image.CircleImageView


/**
 *    @author : EricCui
 *    date   : 2020-01-01 17:52
 *    desc   :
 */

class DetailCommentAdapter (entityList: MutableList<CommentEntity>) : BaseQuickAdapter<CommentEntity,
        RecyclerViewHolder>(R.layout.item_recyclerview_tidbits,entityList){


    override fun convert(helper: RecyclerViewHolder, item: CommentEntity?) {
        if (item != null){
            val icon = helper.getView<ImageView>(R.id.item_tidbits_iv_avatar) as CircleImageView
            Glide.with(mContext)
                    .load(item.userPic)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .placeholder(R.drawable.avatar_placeholder)
                    .error(R.drawable.avatar_placeholder)
                    .into(icon)

            helper.setText(R.id.item_tidbits_tv_user,item.firstName + item.lastName)
            val ivZan = helper.getView<ImageView>(R.id.tidbits_iv_zan) as ImageView
            val ratingBar = helper.getView<TextView>(R.id.item_tidbits_rb) as AndRatingBar
            if (!TextUtils.isEmpty(item.score)) {
                ratingBar.rating = item.score.toFloat()
            }
            helper.setText(R.id.item_tidbits_tv_subtitle,item.title)
            helper.setText(R.id.item_tidbits_tv_about,item.commentText)
            helper.setText(R.id.tidbits_tv_zan_num,item.support)
            if (!TextUtils.isEmpty(item.commentTime)) {
                helper.setText(R.id.item_tidbits_tv_time, getTime(item.commentTime))
            }
            if (item.supportStatus){
                ivZan.setImageResource(R.drawable.detail_zan_after)
            }else {
                ivZan.setImageResource(R.drawable.detail_zan_before)
            }
        }

    }

    private fun getTime(time:String):String{
        val year = time.substring(0, 4).toInt()
        val month = time.substring(4, 6).toInt()
        val day = time.substring(6, 8)
        val hour = time.substring(8, 10)
        val minute = time.substring(10, 12)
        return "$day/$month/$year $hour:$minute"
    }
}